##CODE FOR ANALYSIS OF PLAUSABILITY DATA FOR NATIVE SPEAKERS
##small changes can be made to run the same code on heritage speakers

plausnorm.data <- read.csv("MTURK.plausnorm.data.csv")
PND <- plausnorm.data

##trim unnecessary columns and non-native-Spanish speakers
PND <- PND[,c(-(1:15),-(17:27), -63, -64)]
PND <- PND[PND$Answer.firstlanguage == "spanish" & PND$Answer.homelanguage == "spanish",]
  ##change to "(L1 == spanish and homeL == english) or (L1 == english and homeL == spanish)"
  ##for heritage speakers

##create matrix for final results
avg.plaus.mturk <- mat.or.vec(15,2)
colnames(avg.plaus.mturk) <- c("item", "avg.val")
avg.plaus.mturk[1:15,1] <- c(1:5,7:16)

##this function takes average of scores of column pairs, rearranges according to item order
##and puts them in the final avg.plaus matrix
temp.vals <- mat.or.vec(15,1)
vec.col.nums <- c(seq(2,30, by = 2))
for(i in 1 : 15){
  temp.col <- vec.col.nums[i]
  temp.vals[i] <- mean(c(colMeans(PND[,temp.col:(temp.col + 1)], na.rm = TRUE)))
}
avg.plaus.mturk[1:8,2] <- temp.vals[8:15]
avg.plaus.mturk[9:15,2] <- temp.vals[1:7]
avg.plaus.mturk <- as.data.frame(avg.plaus.mturk)

##d is the matrix of results from the larger experiment

##create a boolean variable that splits sentences into two groups
##according to plausability rating;
##split can be arbitrary, for this analysis we chose a split that
##divided the sentences into two roughly equal groups.
d <- na.omit(d) 
d$plaus.TF <- 0
for(i in 1:dim(d)[1]){ 
  if(d$plaus[i] < 2.5){    ##2.5 is split number; change as desired
    d$plaus.TF[i] <- 1
  }
}

# SG head noun analysis
SG = d[d$N1num=='SG'&d$N1genmatch==1&d$N2genmatch==1,]
aggregate(response1~N1num*N2num*adjnum*plaus.TF,data = SG,mean)
lmer(response1~N1nummatch*N2nummatch*plaus.TF+(1|workerID)+(1|item),data=SG)
lmer(response1~N1nummatch*N2nummatch+(1|workerID)+(1|item),data=SG)

# PL head noun analysis
PL = d[d$N1num=='PL'&d$N1genmatch==1&d$N2genmatch==1,]
aggregate(response1~N1num*N2num*adjnum*plaus.TF,PL,mean)
lmer(response1~N1nummatch*N2nummatch*plaus.TF+(1|workerID)+(1|item),data=PL)
lmer(response1~N1nummatch*N2nummatch+(1|workerID)+(1|item),data=PL)


# M head noun analysis
M.data = d[d$gen1=='M'&d$N1nummatch==1&d$N2nummatch==1,]
aggregate(response1~gen1*gen2*adjgen*plaus.TF, data = M.data ,mean)
lmer(response1~N1genmatch*N2genmatch*plaus.TF+(1|workerID)+(1|item),data=M.data)
lmer(response1~N1genmatch*N2genmatch+(1|workerID)+(1|item),data=M.data)

# F head noun analysis
##must run both lines each time!
F.data = d[d$gen1=='F'&d$N1nummatch==1&d$N2nummatch==1,]
aggregate(response1~gen1*gen2*adjgen*plaus.TF,data = F.data,FUN = mean)
lmer(response1~N1genmatch*N2genmatch*plaus.TF+(1|workerID)+(1|item),data=F.data)
lmer(response1~N1genmatch*N2genmatch+(1|workerID)+(1|item),data=F.data)

